﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Brainee.Search;
using Brainee.Examples.Search;
using System.Threading;

namespace BrainUI
{
  public partial class Search : Form
  {
    public Search ()
    {
      InitializeComponent ();
    }

    private void button1_Click (object sender, EventArgs e)
    {
      VacuumCleaner pb = new VacuumCleaner ();
      pb.InitialState = new VacuumCleaner.State (VacuumCleaner.State.Status.Dirty, VacuumCleaner.State.Status.Dirty, VacuumCleaner.State.Location.Left);

      TreeSearch st = new TreeSearch ();
      st.Strategy = new BreadthFirstStrategy ();

      PathSolution solution = st.Solve (pb);
    }

    private void button2_Click (object sender, EventArgs e)
    {
      EightPuzzle pb = new EightPuzzle ();
      //pb.InitialState = new EightPuzzle.State (new byte [9] { 1, 2, 0, 3, 4, 5, 6, 7, 8 });
      //pb.InitialState = new EightPuzzle.State (new byte [9] { 1, 2, 5, 3, 4, 0, 6, 7, 8 });
      //pb.InitialState = new EightPuzzle.State (new byte [9] { 1, 2, 5, 3, 4, 8, 6, 7, 0 });
      //pb.InitialState = new EightPuzzle.State (new byte [9] { 1, 2, 5, 3, 0, 4, 6, 7, 8 });
      pb.InitialState = new EightPuzzle.State (new byte [9] { 1, 2, 5, 3, 7, 4, 6, 0, 8 });
      //pb.InitialState = new EightPuzzle.State (new byte [9] { 7, 2, 4, 5, 0, 6, 8, 3, 1 });
      
      // Fixed depth-limited strategy
      //ILimitedSearchStrategy ss = new DepthLimitedStrategy ();
      //ss.Limit = 5;

      // Iterative depth-limited strategy
      //ILimitedSearchStrategy dls = new DepthLimitedStrategy ();
      //ISearchStrategy ss = new IterativeDeepeningStrategy (dls);
      
      // graph-search strategy
      ISearchStrategy iss = new BreadthFirstStrategy ();
      ISearchStrategy ss = new GraphSearchStrategy (iss);

      TreeSearch st = new TreeSearch ();
      st.Strategy = ss;
      PathSolution solution = st.Solve (pb);
    }


    private ManualResetEvent _eventContinue;

    private void button3_Click (object sender, EventArgs e)
    {
      if (_eventContinue != null)
      {
        _eventContinue.Set();
        return;
      }

      new Thread (delegate ()
      {
        _eventContinue = new ManualResetEvent (false);

        EightQueens pb = new EightQueens ();
        problemControl1.Solution = null;

        ILimitedSearchStrategy ss = new DepthLimitedStrategy ();
        ss.Limit = 8;

        TreeSearch st = new TreeSearch ();
        st.Strategy = ss;
        st.SolutionFound += delegate (object s, SolutionFoundEventArg ea)
        {
          problemControl1.Solution = ea.Solution;
          _eventContinue.WaitOne ();
          _eventContinue.Reset ();
          ea.Continue = true;
        };


        //ISearchStrategy dls = new DepthLimitedStrategy ();
        //ISearchStrategy ss = new IterativeDeepeningStrategy (dls, 8, 9);
        //SearchTree st = new SearchTree ();


        //ILimitedSearchStrategy ss = new DepthLimitedStrategy ();
        //ss.Limit = 8;
        //RecursiveSearch st = new RecursiveSearch ();

        st.Solve (pb);
        problemControl1.Solution = null;

        _eventContinue = null;
      }).Start ();
    }

    private void button4_Click (object sender, EventArgs e)
    {
      IProblem pb = new RomaniaTrip ();
      pb.InitialState = new RomaniaTrip.In (RomaniaTrip.City.Arad);

      //ISearchStrategy ss = new UniformCostStrategy ();
      //ISearchStrategy ss = new GreedyBestFirstStrategy (pb);
      ISearchStrategy ss = new AStarStrategy (pb);

      TreeSearch sa = new TreeSearch ();
      sa.Strategy = ss;

      PathSolution solution = sa.Solve (pb);
    }
  }
}
